/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hotelsupplymanagement;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.beans.*;
import java.util.*;
import javax.swing.table.DefaultTableModel;

class AddInventory {
    JLabel l1,l2,l3,l4;
    JTextField t1,t2,t3,t4;
    JButton jb1,jb2;
    JPanel jp1,jp2;
    FileOutputStream fos;
    PrintStream ps;

public void addInventory()
	{
            JFrame jf=new JFrame("Addition of a New Inventory Item");
            jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            jf.setBackground(Color.white);
            jf.setSize(400,300);
            jf.setLocation(250,200);
            jf.setVisible(true);
            jf.setLayout(new BorderLayout(30,30));
            JLabel jl=new JLabel("Enter the following details of the inventory item");
            jp1=new JPanel();
            jp1.setLayout(new GridLayout(1,1));
            jp1.add(jl);
            jf.add(jp1,BorderLayout.NORTH);
            jp2=new JPanel();
            jp2.setLayout(new GridLayout(6,2,10,10));

            l1=new JLabel("Name of the Item:");
            jp2.add(l1);
            t1=new JTextField();
            jp2.add(t1);
            l2=new JLabel("Unique ID of the Item:");
            jp2.add(l2);
            t2=new JTextField();
            jp2.add(t2);
            l3=new JLabel("Quantity of the Item:");
            jp2.add(l3);
            t3=new JTextField();
            jp2.add(t3);
            l4=new JLabel("Price of the Item:");
            jp2.add(l4);
            t4=new JTextField();
            jp2.add(t4);
            jb1=new JButton("Add");
            jp2.add(jb1);
            jb2=new JButton("Clear");
            jp2.add(jb2);
            jf.add(jp2,BorderLayout.CENTER);

	jb1.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent e)
	{
                try {
                        savetoFile();
                    } 
                catch (FileNotFoundException ex)
                {
                    Logger.getLogger(AddInventory.class.getName()).log(Level.SEVERE, null, ex);
                } 
                catch (IOException ex) 
                {
                    Logger.getLogger(AddInventory.class.getName()).log(Level.SEVERE, null, ex);
                }
                clear();
	}

	});


	jb2.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent e)
	{
            clear();
	}});

	}

        private void savetoFile() throws FileNotFoundException, IOException
        {
                GetInventories q = new GetInventories();
                fos=new FileOutputStream("GUIItem.txt",true);
		ps=new PrintStream(fos);
                q.setInventoryName(t1.getText());
                q.setInventoryId(Integer.parseInt(t2.getText()));
                q.setInventoryQuantity(Integer.parseInt(t3.getText()));
                q.setInventoryPrice(Integer.parseInt(t4.getText()));
		ps.println(q.getInventoryName()+","+q.getInventoryId()+","+q.getInventoryQuantity()+","+q.getInventoryPrice());
                XMLEncoder e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("GUIItem1.xml",true)));
                e.writeObject(q);
                e.close();
                fos.close();
         }

	public void clear()
	{
            t1.setText("");
            t2.setText("");
            t3.setText("");
            t4.setText("");
            return;
	}

}
